﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Shape
{

	class Filesys
	{

		//String[] filepatharry = null;
	static	List<String> files = new List<String>();
		//创建文件夹
		public static bool CreateFolder(String filepath)
		{

			if (0 == filepath.Trim().Length)
			{
				return false;
			}
			else if (!Directory.Exists(filepath))
			{

				Directory.CreateDirectory(filepath);
				return true;
			}
			else
				return true;

		}

		//遍历文件夹，获取制定fliter的文件路径列表
		public static String[] GetFilepath(String filepath){
		ProcessDirectory(filepath);
			return files.ToArray();
		}
		//遍历文件夹
		public static void ProcessDirectory(string targetDirectory)
		{
			// Process the list of files found in the directory.
			string[] fileEntries = Directory.GetFiles(targetDirectory);
			foreach (string fileName in fileEntries)
				files.Add(fileName);
			// Recurse into subdirectories of this directory.
			string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
			foreach (string subdirectory in subdirectoryEntries)
				ProcessDirectory(subdirectory);
		}
		//统计文件个数
		public static int counterfiles(string[] files){
			return files.Length;
		}

		//获取OpenfileDIalog的path string[];

		public static  string[] getfiles(string[] filenames){
		List<string> filelist = new List<string>();
		foreach(string f in filenames)
		{
			if (!Directory.Exists(f))
			{
				filelist.Add(f);
			}else{
				filelist.AddRange(Filesys.getfiles(f));
			}
				}
			return filelist.ToArray();

		}

		//获取所有shp文件
		public static string[] getfiles(string filepath){ 
			string path = @filepath;
			string searchPattern = "*.shp";
			if (File.Exists(filepath))
			{
			string[] filesall  =	new string[0];
				filesall[0] = filepath;
				return filesall;
			} 
			else
			{
				return Directory.GetFiles(path,searchPattern,SearchOption.AllDirectories);
			}

		}
			//获取所有符合通配符的文件
		public static string[] getfiles(string filepath,string searchPattern){ 
			string path = @filepath;
			
			if (File.Exists(filepath))
			{
			string[] filesall  =	new string[0];
				filesall[0] = filepath;
				return filesall;
			} 
			else
			{
				return Directory.GetFiles(path,searchPattern,SearchOption.AllDirectories);
			}

		}
					//获取所有符合通配模式的当前文件夹
		public static string[] getfiles(string filepath,string searchPattern,SearchOption searchOption){ 
			string path = @filepath;
			
			if (File.Exists(filepath))
			{
			string[] filesall  =	new string[0];
				filesall[0] = filepath;
				return filesall;
			} 
			else
			{
				return Directory.GetFiles(path,searchPattern,searchOption);
			}

		}

		public static bool deletespecifile(string path,string mode){
			if (path.Trim().Length==0)
			{
				return false;
			}
			else if (!Directory.Exists(path))
			{
				File.Delete(path);
				return true;
			} 
			else
			{
			string[] filelist= Directory.GetFiles(path,mode);
 			foreach(string f in filelist)
			File.Delete(f);
			return true;	
			}

		}


	}
	



}
